/**
 * 
 */
package uk.ac.ebi.curtain.utils.data.impl;

import uk.ac.ebi.curtain.utils.data.IdentifierProcessor;
import uk.ac.ebi.velvet.model.Strand;

/**
 * @author mhaimel
 *
 */
public abstract class AbstractIdentifierProcessor implements IdentifierProcessor {

	private static final char ID_CHAR = '@';
	
	protected char getLastChar(String id){
		return id.charAt(id.length()-1);
	}

	@Override
	public Strand getStrand(String id) {
		return getStrandFromId(cleanId(id));
	}
	
	private Strand getStrandFromId(String id) {
		char last = getLastChar(id);
		return Strand.getStrand(last);
	}

	@Override
	public String cleanId(String id) {
		id = id.trim();
		if(id.charAt(0) == ID_CHAR){
			id = id.substring(1);
		}
		return id.toString();
	}
	
	@Override
	public boolean hasStrandInformation(String id) {
		return getStrandFromId(cleanId(id)) != null;
	}

	@Override
	public String removeStrandInformation(String id) {
		return removeStrandInformation(id,true);
	}
	
	@Override
	public String removeStrandInformation(String id,boolean failIfNoStrandInformation) {
		String retId = cleanId(id);
		Strand str = getStrandFromId(retId);
		if(null == str){
			if(failIfNoStrandInformation){
				throw new UnsupportedOperationException("Can't extract Strand information from id: " + id);
			}
		} else {
			retId = new String(retId.substring(0, retId.length()-suffixLength()).toCharArray());
		}
		return retId;
	}
	
	protected abstract int suffixLength();

}
